package com.woody.aggregate.application.service.saas.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.woody.aggregate.application.service.saas.SaasShopConfigService;
import com.woody.aggregate.infrastructure.saas.entity.SaasShopConfig;
import com.woody.aggregate.infrastructure.saas.mapper.SaasShopConfigMapper;
import com.woody.aggregate.saas.dto.AddOrUpdateSaasShopConfigDTO;
import com.woody.aggregate.saas.vo.SaasShopConfigVO;
import com.woody.common.web.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SaasShopConfigServiceImpl extends ServiceImpl<SaasShopConfigMapper, SaasShopConfig> implements SaasShopConfigService {

    @Override
    public SaasShopConfigVO getByShopId(Long shopId) {
        if (shopId == null) {
            return null;
        }
        SaasShopConfig saasShopConfig = ChainWrappers.lambdaQueryChain(getBaseMapper()).eq(SaasShopConfig::getShopId, shopId).one();
        if (saasShopConfig == null) {
            return null;
        }
        SaasShopConfigVO saasShopConfigVO = JacksonUtil.reGroup(saasShopConfig, SaasShopConfigVO.class);
        return saasShopConfigVO;
    }

    @Override
    public Long saveOrUpdate(AddOrUpdateSaasShopConfigDTO addOrUpdateSaasShopConfigDTO) {
        if (addOrUpdateSaasShopConfigDTO == null) {
            return 0L;
        }

        SaasShopConfig saasShopConfig = JacksonUtil.reGroup(addOrUpdateSaasShopConfigDTO, SaasShopConfig.class);
        this.saveOrUpdate(saasShopConfig);
        return saasShopConfig.getId();
    }
}
